//
//  MYShopGoodsInsuranceTestifyImageCell.swift
//  MuYing
//
//  Created by tbb on 2019/7/31.
//  Copyright © 2019 TBB. All rights reserved.
//

import UIKit


@objc protocol MYShopGoodsInsuranceTestifyImageCellDelegate:NSObjectProtocol {
    
    func didSelectItemAtImageAction(_ indexPath:NSIndexPath);
    
}

class MYShopGoodsInsuranceTestifyImageCell: UITableViewCell,UICollectionViewDataSource,UICollectionViewDelegate {
    @objc weak var delegate: MYShopGoodsInsuranceTestifyImageCellDelegate?
    @objc var imageArr = [UIImage]() {
        didSet {
            self.collView.reloadData();
        }
    }
    
    lazy var collView: UICollectionView = {
        let layout = UICollectionViewFlowLayout();
        layout.scrollDirection = .horizontal;
        layout.itemSize = CGSize(width: 85, height: 85);
        layout.minimumLineSpacing = 0.001;
        layout.minimumLineSpacing = 17;
        let collView = UICollectionView(frame: CGRect(x: 7, y: 14, width: kSwiftkScreenSizeWidth-14, height: 85), collectionViewLayout: layout);
        collView.backgroundColor = UIColor.white;
        collView.delegate = self;
        collView.dataSource = self;
        collView.bounces = false;
        collView.register(UINib.init(nibName: "MYShopGoodsInsuranceTestifyImageCollCell", bundle: nil), forCellWithReuseIdentifier: "MYShopGoodsInsuranceTestifyImageCollCell")
        return collView;
    }()
    
    override func layoutSubviews() {
        super.layoutSubviews();
        self.contentView.addSubview(self.collView);
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension MYShopGoodsInsuranceTestifyImageCell {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.imageArr.count;
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MYShopGoodsInsuranceTestifyImageCollCell", for: indexPath) as? MYShopGoodsInsuranceTestifyImageCollCell else { return UICollectionViewCell() };
        cell.testifyImageView.image = self.imageArr[indexPath.item];
        return cell;
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let delegate = self.delegate, delegate.responds(to: Selector(("didSelectItemAtImageAction:"))){
            self.delegate?.didSelectItemAtImageAction(indexPath as NSIndexPath);
        }
    }
}
